﻿#region zh-CHS 2006 - 2010 DemoSoft 团队 | en 2006-2010 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the MMOSE(Massively Multiplayer Online Server Engine) for .NET.
//
//                              2006-2010 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using Demo.Mmose.Core.Common;
using Demo.Mmose.Core.Common.Template;
using Demo.Mmose.Core.Map;
#endregion

namespace Demo.Mmose.Core.Entity.GameObject
{
    /// <summary>
    /// 
    /// </summary>
    public class GameObjectBase : WorldEntity, IComparable, IComparable<GameObjectBase>
    {
        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public GameObjectBase()
        {
            DefaultGameObjectInit();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="serial">唯一序号</param>
        public GameObjectBase( Serial serial )
            : base( serial )
        {
            DefaultGameObjectInit();
        }

        #region zh-CHS 人物的初始化 | en
        /// <summary>
        /// 缺省的初始化人物
        /// </summary>
        protected virtual void DefaultGameObjectInit()
        {
        }
        #endregion

        #endregion

        #region zh-CHS 共有属性 | en Public Properties

        #region zh-CHS Name属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 人物的名字
        /// </summary>
        private string m_Name = string.Empty;
        #endregion
        /// <summary>
        /// 人物的名字
        /// </summary>
        public string Name
        {
            get { return m_Name; }
            set
            {
                string oldName = m_Name;

                //if ( m_BaseGameObjectState != null )
                //{
                //    if ( m_BaseGameObjectState.OnUpdatingName( value, this ) == true )
                //        return;
                //}

                m_Name = value;
                //m_BaseGameObjectState.IsUpdateName = true;

                //if ( m_BaseGameObjectState != null )
                //    m_BaseGameObjectState.OnUpdatedName( oldName, this );
            }
        }

        #endregion

        #region zh-CHS GameObjectState属性 | en Public Properties

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 人物的状态机
        /// </summary>
        private GameObjectBaseState m_BaseGameObjectState = null;
        #endregion
        /// <summary>
        /// BaseCreatureState人物的状态器
        /// </summary>
        public GameObjectBaseState BaseGameObjectState
        {
            get { return m_BaseGameObjectState; }
            protected set { m_BaseGameObjectState = value; }
        }

        #endregion

        #endregion

        #region zh-CHS 游戏对象的移动 | en
        /// <summary>
        /// 人物的移动方向
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public override bool MoveTo( Point3D newLocation )
        {
            MapBase tempMap = this.Map; // 副本防止多线程置空
            if ( this.Deleted == true || tempMap == null )
                return false;

            Point3D oldLocation = (Point3D)this.Location;

            this.Location = newLocation; // 间接设置
            tempMap.OnMove( oldLocation, this );

            return true;
        }

        /// <summary>
        /// 移动到世界的某地图的某个点
        /// </summary>
        /// <param name="newLocation"></param>
        /// <param name="map"></param>
        public override bool MoveToWorld( Point3D newLocation, MapBase newMap )
        {
            MapBase tempMap = this.Map; // 副本防止多线程置空
            if ( this.Deleted == true || tempMap == null || newMap == null )
                return false;

            if ( tempMap == newMap )
                return MoveTo( newLocation );
            else
            {
                MapBase oldMap = tempMap;
                Point3D oldLocation = (Point3D)this.Location;

                this.Map = newMap; // 间接设置
                this.Location = newLocation; // 间接设置

                oldMap.OnLeave( this );
                tempMap.OnEnter( this );

                return true;
            }
        }
        #endregion

        #region zh-CHS 创建一个新的自身 | en Public Method
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public GameObjectBase CreateNewGameObjectInstance()
        {
            return CreateNewInstance<GameObjectBase>();
        }
        #endregion

        #region zh-CHS 接口实现 | en Interface Implementation
        /// <summary>
        /// 
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public int CompareTo( GameObjectBase other )
        {
            return CompareTo( other as WorldEntity );
        }
        #endregion

        #region zh-CHS 方法覆盖 | en Override Methods
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return m_Name;
        }
        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public class GameObjectBase<GameObjectTemplateT> : GameObjectBase, ITemplateSerial
        where GameObjectTemplateT : GameObjectTemplateBase
    {
        #region zh-CHS GameObjectTemplate属性 | en Public Properties

        /// <summary>
        /// 模板的Id
        /// </summary>
        /// <returns></returns>
        public Serial TemplateSerial
        {
            get
            {
                GameObjectTemplateT gameObjectTemplate = m_GameObjectTemplate;
                if ( gameObjectTemplate == null )
                    return Serial.MinusOne;
                else
                    return gameObjectTemplate.Serial;
            }
        }

        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 人物的帐号等级
        /// </summary>
        private GameObjectTemplateT m_GameObjectTemplate = default( GameObjectTemplateT );
        #endregion
        /// <summary>
        /// 人物的帐号等级
        /// </summary>
        public GameObjectTemplateT GameObjectTemplate
        {
            get { return m_GameObjectTemplate; }
            set
            {
                GameObjectTemplateT oldGameObjectTemplate = m_GameObjectTemplate;

                //if ( m_BaseItemState != null )
                //{
                //    if ( m_BaseItemState.OnUpdatingItemTemplate( value, this ) == true )
                //        return;
                //}

                m_GameObjectTemplate = value;
                //m_BaseItemState.IsUpdateItemTemplate = true;

                //if ( m_BaseItemState != null )
                //    m_BaseItemState.OnUpdatedItemTemplate( oldItemTemplate, this );
            }
        }

        #endregion
    }
}
#endregion